package org.linphone.activity;

import java.util.List;

import org.linphone.LinphoneActivity;
import org.linphone.LinphoneManager;
import org.linphone.LinphoneService;
import com.leehuubsd.yiyanzb.R;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;

import org.linphone.Bean.LandedDate;
import org.linphone.core.LinphoneCore;
import org.linphone.setup.SetupActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SettingActivity extends Activity implements OnClickListener {

	private static final int WIZARD_INTENT = 1;
	private CheckBox mAutoAnswer,i9300;
//	private CheckBox mAutoAns;

	private SharedPreferences mSharedPreferences;
	private SharedPreferences sharedPreferences;
	private Editor edit;
	public static final String SETTING_AUTO_ANSWER = "setting_auto_answer";
	private Button btn1, btn2;
	private View mBackBtn;
	private RelativeLayout setting_modify, activity_more_relat5,huadan,fankui,拦截检测;

	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		System.out.println(android.os.Build.BRAND);

		sharedPreferences = getSharedPreferences("bosideng",
				Context.MODE_WORLD_READABLE);
		edit = sharedPreferences.edit();
		find();
		if (!(android.os.Build.BRAND).equals("Meizu")) {
		}
		mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);

		mBackBtn = findViewById(R.id.setting_back);
		mBackBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();

			}
		});

//		mAutoAns = (CheckBox) findViewById(R.id.cb_toMyAccount);
//		mAutoAns.setChecked(sharedPreferences.getBoolean("call", false));
//		mAutoAns.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView,
//					boolean isChecked) {
//				if (isChecked) {
//					StartService();
//				} else {
//					StopService();
//				}
//				edit.putBoolean("call", isChecked);
//				edit.commit();
//			}
//		});
		i9300 = (CheckBox) findViewById(R.id.i9300);
		i9300.setChecked(sharedPreferences.getBoolean("i9300", true));
		i9300.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				edit.putBoolean("i9300",isChecked);
				edit.commit();
				
			}
		});

		mAutoAnswer = (CheckBox) findViewById(R.id.setting_auto_answer);
		mAutoAnswer.setChecked(mSharedPreferences.getBoolean(
				SettingActivity.SETTING_AUTO_ANSWER, false));
		mAutoAnswer.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Editor editor = mSharedPreferences.edit();
				editor.putBoolean(SETTING_AUTO_ANSWER, isChecked);
				editor.commit();
			}
		});
	}


	// private void startSelectHomeDialog() {
	// IntentFilter filter = new IntentFilter();
	// filter.addAction(Intent.ACTION_CALL_BUTTON);
	// filter.addCategory(Intent.CATEGORY_DEFAULT);
	// PackageManager manager = getPackageManager();
	// Intent intent = new Intent(Intent.ACTION_CALL_BUTTON, null);
	// intent.addCategory(Intent.CATEGORY_DEFAULT);
	// List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);
	// System.out.println(list);
	// for (int i = 0; i < list.size(); i++) {
	// // 作用是： 清除之前选择的Homescreen,比如即使你手动设置了Launcher作为你的
	// //Homescreen，执行以下方法之后，Launcher就不再是默认的Homescreen了。
	// manager.clearPackagePreferredActivities(list.get(i).activityInfo.packageName);
	// }
	//
	// Intent intent2 = new Intent(Intent.ACTION_MAIN);
	// intent2.addCategory(Intent.CATEGORY_HOME);
	// startActivity(intent2);
	// }

	private void find() {
		btn1 = (Button) findViewById(R.id.backapk);
		btn2 = (Button) findViewById(R.id.cancell);
		fankui = (RelativeLayout) findViewById(R.id.fankui);
		setting_modify = (RelativeLayout) findViewById(R.id.setting_modify);
		activity_more_relat5 = (RelativeLayout) findViewById(R.id.activity_more_relat5);
		huadan = (RelativeLayout) findViewById(R.id.huadan);
		拦截检测 = (RelativeLayout) findViewById(R.id.拦截检测);
		
		拦截检测.setOnClickListener(this);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		activity_more_relat5.setOnClickListener(this);
		huadan.setOnClickListener(this);
		fankui.setOnClickListener(this);
		setting_modify.setOnClickListener(this);

	}

	public String meizhu() {
		String s = android.os.Build.BRAND;
		return s;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.拦截检测:
			Intent intent拦截检测 = new Intent(SettingActivity.this, SafeActivity.class);
			startActivity(intent拦截检测);
			break;
		case R.id.fankui:
			FeedbackAgent agent = new FeedbackAgent(this);
		    agent.startFeedbackActivity();
			break;
		case R.id.backapk:
			showTips();
			break;
		case R.id.setting_modify:
			if (getLandName()) {
				Intent intentm = new Intent(SettingActivity.this,
						ModifyWordActivity.class);
				startActivity(intentm);
			} else {
				Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(LinphoneService.instance(), SetupActivity.class);
				startActivityForResult(intent, WIZARD_INTENT);
			}
			break;
		case R.id.activity_more_relat5:
			Intent intent3 = new Intent(this, WebsiteActivity.class);
			intent3.putExtra("pathweb","http://faqbosideng.leehuu.cn/index-common.html");
			intent3.putExtra("title","常见问题");
			startActivity(intent3);
			break;
		case R.id.huadan:
			Intent intent4 = new Intent(this, WebsiteActivity.class);
			String namees = sharedPreferences.getString("Phone", null);
			String CardPassWord = sharedPreferences.getString("CardPassWord", null);
			intent4.putExtra("pathweb","http://kf.voip-oem.com/BlueWisdom.VOS/VosAccountInfo/WeChatSelectCdr?phone="+namees+"&password="+CardPassWord+"&proxyId="+getResources().getString(R.string.oem));
			intent4.putExtra("title","话单查询");
			startActivity(intent4);
			break;
		case R.id.cancell:
			if (!getLandName()) {
				Toast.makeText(getApplicationContext(), "请先登录", 0).show();
				Intent intent = new Intent(LinphoneService.instance(), SetupActivity.class);
				startActivityForResult(intent, WIZARD_INTENT);
			} else {
				Toast.makeText(getApplicationContext(), "注销成功", 0).show();
				// edit.putString("Phone", null);
				// edit.putString("CardNo",null);
				// edit.putString("CardPassWord", null);
				// edit.commit();
				try {
					LinphoneActivity.instance().displaySettings();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				edit.clear();
				edit.commit();
				finish();
				Intent intent = new Intent(LinphoneService.instance(), SetupActivity.class);
				startActivityForResult(intent, WIZARD_INTENT);
				startActivity(intent);
			}
			break;

		default:
			break;
		}

	}

	private boolean getLandName() {
		LandedDate landedDate = new LandedDate(this);
		String passWord = landedDate.getCardPassword();
		String phone = landedDate.getPhone();
		String cardno = landedDate.getCardNo();
		if (passWord == null || phone == null || cardno == null) {
			return false;
		}
		return true;
	}

	private void showTips() {

		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提醒")
				.setMessage("是否退出程序")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						try {
							LinphoneActivity.instance().exit();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Intent intent = new Intent(Intent.ACTION_MAIN);
						intent.addCategory(Intent.CATEGORY_HOME);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}

				}).setNegativeButton("取消",

				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				}).create(); // 创建对话框
		alertDialog.show(); // 显示对话框
	}

	public class SmsBroadCastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			Object[] object = (Object[]) bundle.get("pdus");
			SmsMessage sms[] = new SmsMessage[object.length];
			for (int i = 0; i < object.length; i++) {
				sms[0] = SmsMessage.createFromPdu((byte[]) object[i]);
				Toast.makeText(
						context,
						"来自" + sms[i].getDisplayOriginatingAddress() + " 的消息是："
								+ sms[i].getDisplayMessageBody(),
						Toast.LENGTH_SHORT).show();
			}
			// 终止广播，在这里我们可以稍微处理，根据用户输入的号码可以实现短信防火墙。
			abortBroadcast();
		}
	}

}
